package com.zslibrary.Tools;
import java.math.BigInteger;

public class HexBytesUtil {

    private static byte uniteBytes(byte src0, byte src1) {
        byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
                .byteValue();
        _b0 = (byte) (_b0 << 4);
        byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
                .byteValue();
        byte ret = (byte) (_b0 ^ _b1);
        return ret;
    }

    public static byte[] HexString2Bytes(String src) {
        byte[] ret = new byte[src.length() / 2];
        byte[] tmp = src.getBytes();
        for (int i = 0; i < tmp.length / 2; i++) {
            ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
        }
        return ret;
    }

    public static StringBuilder byte2HexStr(byte[] data,int size) {

        if (data != null && size > 0) {
            StringBuilder stringBuilder = new StringBuilder(data.length);
            for (int i=0;i<size;i++) {
                stringBuilder.append(String.format("%02X ", data[i]));
            }
            return stringBuilder;
        }
        return null;
    }

    public static String bytesToAllHex(byte[] bytes, int radix) {
        return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
    }

    public static int Bytes2Int(byte[] bytes){
        return ((bytes[0]&0xFF)<<24)|((bytes[1]&0xFF)<<16)|((bytes[2]&0xFF)<<8)|(bytes[3]&0xFF);
    }

}
